#include "GetVarGraph.h"
#include "base/Utility.h"
#include "OutputParaPin.h"
#define SetAttr(AttrName) SetAttribute(AttrName, ele->Attribute(AttrName));
bool GetVarGraph::OnInit()
{
    return false;
}

void GetVarGraph::InitParaPin()
{
    if (opp == nullptr)
    {
        opp = NewObj<OutputParaPin>();
        opp->indexInFunc = 0;
        opp->gbParent = this;
    }
}

void GetVarGraph::ParseXmlElement(TiXmlElement* ele)
{
    SetAttr("id")
        SetAttr("var")
        SetAttr("pos")
        SetAttr("size")
}

bool GetVarGraph::SetAttribute(const char* attrName, const char* attrVal)
{
    if (strcmp("id", attrName) == 0)
    {
        this->id = atoi(attrVal);
    }
    else  if (strcmp("pos", attrName) == 0)
    {
        int x, y;
        scanf_s("%d,%d", &x, &y);
        pos.x.val = x;
        pos.y.val = y;
    }
    else  if (strcmp("size", attrName) == 0)
    {
        int x, y;
        scanf_s("%d,%d", &x, &y);
        size.x.val = x;
        size.y.val = y;
    }
    else  if (strcmp("var", attrName) == 0)
    {
        varName.val = attrVal;
    }
    return false;
}

void GetVarGraph::OnDraw()
{
    if (opp == nullptr)
    {
        opp = NewObj<OutputParaPin>();
        opp->indexInFunc = 0;
        opp->gbParent = this;
    }
    glPushMatrix();
    glTranslatef(baseOffset.x.val, baseOffset.y.val, zIndex.val);
    glLineWidth(1);
    DrawBorder();
    std::wstring wstr = Utility::ConvertToWstring(varName.val);
    wchar_t* str = const_cast<wchar_t*>(wstr.data());
    TrueTypeFont::GetInst()->drawText(str, pos.x.val + size.x.val / 4, pos.y.val + 50,zIndex.val, size.x.val, 20);
    opp->pos.x = pos.x.val + size.x.val - 10;
    opp->pos.y = pos.y.val + 0 * 6 + (0 + 1) * size.y.val / (1 + 1);

    opp->Draw();
    glPopMatrix();

}

void GetVarGraph::onMouseButtonDown(int button, int x, int y)
{
    __super::onMouseButtonDown(button, x, y);
    Vec2vbi point;
    point.x.val = x;
    point.y.val = y;
    if (opp && opp->HitTest(vec2(point.x.val, point.y.val)))
    {
        opp->onMouseButtonDown(button, x, y);
    }
}

void GetVarGraph::onMouseMove(int x, int y)
{
    __super::onMouseMove(x, y);
    Vec2vbi point;
    point.x.val = x;
    point.y.val = y;
    if (opp && opp->HitTest(vec2(point.x.val, point.y.val)))
    {
        opp->onMouseMove(x, y);
    }
}

void GetVarGraph::onMouseButtonUp(int button, int x, int y)
{
    __super::onMouseButtonUp(button, x, y);
    Vec2vbi point;
    point.x.val = x;
    point.y.val = y;
    if (opp->HitTest(vec2(point.x.val, point.y.val)))
    {
        opp->onMouseButtonUp(button, x, y);
    }
}

BlueprintScriptLibrary::Class* GetVarGraph::GetClass()
{
    return nullptr;
}

Vec2vbi GetVarGraph::GetOutputPinPos(int index)
{
    return Vec2vbi();
}

void GetVarGraph::Execute()
{
}

FunctionPin* GetVarGraph::GetFunctionPin()
{
    return nullptr;
}

void GetVarGraph::ConnectTo(GraphBase* graph, int index)
{
}
